﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace SSCAI.DAL
{
    public class HistoricDataHelper
    {
        public static string SolutionId = "A";

        public static XDocument LoadXmlFile()
        {
            return XDocument.Load(GetXmlFile());
        }

        public static bool IsEmptyFile()
        {
            var doc = LoadXmlFile();
            return doc.Root.LastNode == null;
        }

        public static string GetLastIssue()
        {
            var doc = LoadXmlFile();
            return doc.Root.Elements("item").LastOrDefault().Attribute("issue").Value;
        }       

        public static string GetXmlFile()
        {
            string path = Path.Combine(Environment.CurrentDirectory, @"Historic\", DateTime.Now.Year.ToString("0000"), DateTime.Now.Month .ToString("00"));
            HistoricDataHelper.CreateDirectory(path);

            string file = path + "\\" + HistoricDataHelper.SolutionId + "_" + DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + ".xml";
            HistoricDataHelper.CreateFile(file);
            return file;
        }


        public static void InsertLottery(string issue, string lottery, string count, string iscorrect)
        {
            var doc = LoadXmlFile();
            XElement item = CreateItem(issue, lottery, count, iscorrect);
            doc.Root.AddFirst(item);
            doc.Save(GetXmlFile());
        }

        public static void InsertLottery(XDocument doc, XNode lastNode, string issue, string lottery, string count, string iscorrect)
        {
            XElement item = CreateItem(issue, lottery, count, iscorrect);
            lastNode.AddAfterSelf(item);
            doc.Save(GetXmlFile());
        }

        public static XNode GetLastNode()
        {
            var doc = LoadXmlFile();
            return doc.LastNode;
        }

        public static XElement CreateItem(string issue, string lottery, string count, string iscorrect)
        {
            return new XElement("item",
                new XAttribute("issue", issue),
                new XAttribute("lottery4d", lottery),
                new XAttribute("count", count),
                new XAttribute("iscorrect", iscorrect),
                new XAttribute("time", DateTime.Now.ToString()));
        }

        public static void CreateDirectory(string dirPath)
        {
            string[] paths = dirPath.Split('\\');
            string path = paths[0];
            for (int i = 1; i < paths.Length; i++)
            {
                path = path + "\\" + paths[i];
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

        public static void CreateFile(string path)
        {
            if (!File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                XmlElement rootNode = xmlDoc.CreateElement("items");
                xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
                xmlDoc.AppendChild(rootNode);

                xmlDoc.Save(path);
            }
        }
    }
}
